package com.wutuo.aggragation.rollcall.dao;

import cn.apestar.core.database.BaseMapper;
import com.wutuo.aggragation.rollcall.domain.TaskDetail;
import com.wutuo.child.provider.enums.RollCallState;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface TaskDetailMapper extends BaseMapper {

    @Delete({ "delete from task_detail", "where id = #{id,jdbcType=BIGINT}" })
    int deleteByPrimaryKey(Long id);

    @Insert({ "insert into task_detail (id, task_id, ", "user_id, state, create_by, ", "create_date, update_by, ",
        "update_date, remarks, ", "del_flag)", "values (#{id,jdbcType=BIGINT}, #{taskId,jdbcType=BIGINT}, ",
        "#{userId,jdbcType=BIGINT}, #{state,jdbcType=VARCHAR}, #{createBy,jdbcType=BIGINT}, ",
        "#{createDate,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=BIGINT}, ",
        "#{updateDate,jdbcType=TIMESTAMP}, #{remarks,jdbcType=VARCHAR}, ", "#{delFlag,jdbcType=CHAR})" })
    @SelectKey(before = false, keyProperty = "id", resultType = Long.class, statement = "SELECT LAST_INSERT_ID() AS id")
    int insert(TaskDetail record);

    @InsertProvider(type = TaskDetailSqlProvider.class, method = "insertSelective")
    @SelectKey(before = false, keyProperty = "id", resultType = Long.class, statement = "SELECT LAST_INSERT_ID() AS id")
    int insertSelective(TaskDetail record);

    @Select({ "select", TaskDetailSqlProvider.TASKDETAIL_COLUMNS, "from task_detail",
        "where id = #{id,jdbcType=BIGINT}" })
    @ResultType(value = TaskDetail.class)
    TaskDetail selectByPrimaryKey(Long id);

    @SelectProvider(type = TaskDetailSqlProvider.class, method = "selectByChildIds")
    @ResultType(value = TaskDetail.class)
    List<TaskDetail> selectByChildIds(@Param("childIds") List<Long> childIds, @Param("taskId") Long taskId,
                                      @Param("teacherId") Long teacherId);

    @SelectProvider(type = TaskDetailSqlProvider.class, method = "selectByChildIdAndTaskId")
    @ResultType(value = TaskDetail.class)
    TaskDetail selectByChildIdAndTaskId(@Param("childId") Long childId, @Param("taskId") Long taskId);

    @SelectProvider(type = TaskDetailSqlProvider.class, method = "countSignedChildren")
    int countSignedChildren(@Param("state") String state, @Param("taskId") Long taskId,
                            @Param("teacherId") Long teacherId);

    @UpdateProvider(type = TaskDetailSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(TaskDetail record);

    @Update({ "update task_detail", "set task_id = #{taskId,jdbcType=BIGINT},", "user_id = #{userId,jdbcType=BIGINT},",
        "state = #{state,jdbcType=VARCHAR},", "create_by = #{createBy,jdbcType=BIGINT},",
        "create_date = #{createDate,jdbcType=TIMESTAMP},", "update_by = #{updateBy,jdbcType=BIGINT},",
        "update_date = #{updateDate,jdbcType=TIMESTAMP},", "remarks = #{remarks,jdbcType=VARCHAR},",
        "del_flag = #{delFlag,jdbcType=CHAR}", "where id = #{id,jdbcType=BIGINT}" })
    int updateByPrimaryKey(TaskDetail record);

    @Update({ "update task_detail set del_flag = 0, update_by = #{updateBy,jdbcType=BIGINT} ",
        " where task_id = #{taskId,jdbcType=BIGINT} and user_id = #{userId,jdbcType=BIGINT} and del_flag = 1" })
    int delSignRecord(@Param("taskId") Long taskId, @Param("userId") Long userId, @Param("updateBy") Long updateBy);

    @SelectProvider(type = TaskDetailSqlProvider.class, method = "listChildIdsByState")
    List<Long> listChildIdsByState(@Param("state") RollCallState state, @Param("taskId") Long taskId,
                                   @Param("teacherId") Long teacherId);
}
